﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Wwtfly.BBS.Framework.Caches;
using Wwtfly.BBS.BackBLL.Plays;
using Wwtfly.Utility;
using System.Web.UI.HtmlControls;
namespace Wwtfly.BBS.ForeApplication.WebForm
{
    public partial class Lottery3D_ZHFB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridViewBind();
        }
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.SetRenderMethodDelegate(new RenderMethod(DrawGridHeader));
            }

            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.SetRenderMethodDelegate(new RenderMethod(DrawGridHeader));
            }
        }

        protected void DrawGridHeader(HtmlTextWriter output, Control ctl)
        {
            output.Write("<td rowspan='2'　 bgcolor='#e8f1f8'>期号</td>");
            output.Write("<td rowspan='2'　 bgcolor='#e8f1f8'>开奖号码</td>");
            output.Write("<td colspan='10'　 bgcolor='#e8f1f8' height='26px'>号码分布图</td>");
            output.Write("<td rowspan='2'　 bgcolor='#e8f1f8'>组三间隔</td>");
            output.Write("<td rowspan='2'　 bgcolor='#e8f1f8'>和值</td>");
            output.Write("<td colspan='10'　 bgcolor='#e8f1f8'>和值尾数走势图</td>");
            output.Write("<td rowspan='2'　 bgcolor='#e8f1f8'>012路</td>");
            output.Write("<td colspan='10'　 bgcolor='#e8f1f8'>第一位</td>");
            output.Write("<td colspan='10'　 bgcolor='#e8f1f8'>第二位</td>");
            output.Write("<td colspan='10'　 bgcolor='#e8f1f8'>第三位</td>");

            output.Write("<tr  bgcolor='#e8f1f8' height='18px'>");

            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 10; i++)
                {
                    output.Write("<td>");
                    output.Write(i.ToString() + "</td>");
                }
            }
        }

        protected void GridViewBind()
        {
            DataTable dt = new DataTable();
            dt = SSQ_DX_Select(30, "", ""); //，默认显示30期

            if (dt == null || dt.Rows.Count < 1)
            {
                //数据库里没有此彩种开奖号码信息！;
            }
            else
            {
                int k = dt.Rows.Count;
                tb1.Text = dt.Rows[0]["Isuse"].ToString();
                tb2.Text = dt.Rows[k - 1]["Isuse"].ToString();
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
            ColorBind();
        }
        public DataTable SSQ_DX_Select(int num, string str1, string str2)
        {
            DataTable dt = null;
            DataTable dt1 = null;

            dt = CacheHelper.Get("3D_ZHFB_Select") as DataTable;

            if (dt == null || dt.Rows.Count < 1)
            {
                dt = PlaysBLL.Select3DZHFBListWinber();
                CacheHelper.Insert("3D_ZHFB_Select", dt);
            }

            dt1 = dt.Clone();

            //通过期号来查询
            if (num == 0)
            {
                int i = str1.AsInt(0);
                int j = str2.AsInt(0);

                DataRow[] dr2 = dt.Select("Isuse>=" + i + " and  Isuse<=" + j);

                foreach (DataRow r in dr2)
                {
                    dt1.Rows.Add(r.ItemArray);
                }
            }

            //通过按纽选择查询多少期
            else
            {
                if (num >= dt.Rows.Count)
                {
                    num = dt.Rows.Count;
                }

                int ii = dt.Rows.Count;                           //总记录数
                int jj = ii - num;

                DataRow[] drr = dt.Select("id > " + jj + " and id <= " + ii);

                foreach (DataRow rr in drr)
                {
                    dt1.Rows.Add(rr.ItemArray);
                }
            }

            return dt1;
        }
        //搜索
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (tb1.Text == "" || tb1.Text == null || tb2.Text == "" || tb2.Text == null)
            {
                Response.Write("<script> alert('请输入起止期数！');</script>");
            }

            int i = tb1.Text.AsInt(0);
            int j = tb2.Text.AsInt(0);
            if (i > j)
            {
                Response.Write("<script> alert('起始期数输入有误，请查证在输入！');</script>");
                return;
            }

            DataTable dt = null;

            dt = SSQ_DX_Select(0, tb1.Text.ToString(), tb2.Text.ToString());

            if (dt == null || dt.Rows.Count < 1)
            {
                Response.Write("<script> alert('数据库里没有此彩种开奖号码信息！');</script>");
            }
            else
            {
                int k = dt.Rows.Count;
                tb1.Text = dt.Rows[0]["Isuse"].ToString();
                tb2.Text = dt.Rows[k - 1]["Isuse"].ToString();
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
            ColorBind();
        }
        //显示30
        protected void Button2_Click(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();
            dt = SSQ_DX_Select(30, "", ""); //，默认显示30期

            if (dt == null || dt.Rows.Count < 1)
            {
                //数据库里没有此彩种开奖号码信息!;
            }
            else
            {
                int k = dt.Rows.Count;

                tb1.Text = dt.Rows[0]["Isuse"].ToString();
                tb2.Text = dt.Rows[k - 1]["Isuse"].ToString();
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
            ColorBind();
        }
        //显示50
        protected void Button3_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = SSQ_DX_Select(50, "", ""); //，默认显示50期

            if (dt == null || dt.Rows.Count < 1)
            {
                //数据库里没有此彩种开奖号码信息!;
            }
            else
            {
                int k = dt.Rows.Count;
                tb1.Text = dt.Rows[0]["Isuse"].ToString();
                tb2.Text = dt.Rows[k - 1]["Isuse"].ToString();
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
            ColorBind();
        }
        //显示100
        protected void Button4_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = SSQ_DX_Select(100, "", ""); //，默认显示100期

            if (dt == null || dt.Rows.Count < 1)
            {
                //数据库里没有此彩种开奖号码信息!;
            }
            else
            {
                int k = dt.Rows.Count;
                tb1.Text = dt.Rows[0]["Isuse"].ToString();
                tb2.Text = dt.Rows[k - 1]["Isuse"].ToString();
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
            ColorBind();
        }

        protected void ColorBind()
        {
            lbline.Text = "<script type=\"text/javascript\">function DrawLines(){";
            string ImgID = "";
            string ImgID1 = "";
            string ImgID2 = "";
            string ImgID3 = "";

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[12].Text.AsInt(0) == 0)
                {
                    HtmlImage hi = new HtmlImage();
                    hi.Src = "Images/Qimg/4[1].jpg";
                    GridView1.Rows[i].Cells[12].Controls.Add(hi);
                }


                for (int j = 14; j < 24; j++)
                {
                    if (GridView1.Rows[i].Cells[j].Text.AsInt(0) == 0)
                    {
                        Label lb = new Label();
                        lb.Text = "<strong style='color:red;' id='" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "'>" + (j - 14).ToString();

                        if (ImgID != null && ImgID != "")
                        {
                            lbline.Text += "DrawLine('" + ImgID + "','" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "', '#A9A9A9');";
                        }

                        GridView1.Rows[i].Cells[j].Controls.Add(lb);

                        ImgID = "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString();
                    }
                }

                for (int j = 25; j < 35; j++)
                {
                    if (GridView1.Rows[i].Cells[j].Text.AsInt(0) == 0)
                    {
                        Label lb = new Label();
                        lb.Text = "<strong style='color:blue;' id='" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "'>" + (j - 25).ToString();

                        if (ImgID1 != null && ImgID1 != "")
                        {
                            lbline.Text += "DrawLine('" + ImgID1 + "','" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "', '#A9A9A9');";
                        }

                        GridView1.Rows[i].Cells[j].Controls.Add(lb);

                        ImgID1 = "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString();
                    }
                }

                for (int j = 35; j < 45; j++)
                {
                    if (GridView1.Rows[i].Cells[j].Text.AsInt(0) == 0)
                    {
                        Label lb = new Label();
                        lb.Text = "<strong style='color:red;' id='" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "'>" + (j - 35).ToString();

                        if (ImgID2 != null && ImgID2 != "")
                        {
                            lbline.Text += "DrawLine('" + ImgID2 + "','" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "', '#A9A9A9');";
                        }

                        GridView1.Rows[i].Cells[j].Controls.Add(lb);

                        ImgID2 = "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString();
                    }
                }

                for (int j = 45; j < 55; j++)
                {
                    if (GridView1.Rows[i].Cells[j].Text.AsInt(0) == 0)
                    {
                        Label lb = new Label();
                        lb.Text = "<strong style='color:red;' id='" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "'>" + (j - 45).ToString();

                        if (ImgID3 != null && ImgID3 != "")
                        {
                            lbline.Text += "DrawLine('" + ImgID3 + "','" + "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString() + "', '#A9A9A9');";
                        }

                        GridView1.Rows[i].Cells[j].Controls.Add(lb);

                        ImgID3 = "GridView1_ctl" + (i + 2).ToString().PadLeft(2, '0') + "_" + i.ToString() + "_" + j.ToString();
                    }
                }

                for (int j = 2; j < 12; j++)
                {
                    if (GridView1.Rows[i].Cells[j].Text.AsInt(-1) == -1)
                    {
                        GridView1.Rows[i].Cells[j].Text = "&nbsp;";
                    }
                }

                string str = GridView1.Rows[i].Cells[1].Text;
                int ii = int.Parse(str.Substring(0, 1));
                int jj = int.Parse(str.Substring(1, 1));
                int kk = int.Parse(str.Substring(2, 1));


                if (ii == jj || ii == kk)
                {
                    int m = ii;
                    if (m == 0)
                    {
                        GridView1.Rows[i].Cells[2].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 1)
                    {
                        GridView1.Rows[i].Cells[3].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 2)
                    {
                        GridView1.Rows[i].Cells[4].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 3)
                    {
                        GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 4)
                    {
                        GridView1.Rows[i].Cells[6].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 5)
                    {
                        GridView1.Rows[i].Cells[7].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 6)
                    {
                        GridView1.Rows[i].Cells[8].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 7)
                    {
                        GridView1.Rows[i].Cells[9].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 8)
                    {
                        GridView1.Rows[i].Cells[10].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else
                    {
                        GridView1.Rows[i].Cells[11].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                }
                if (jj == kk)
                {
                    int m = jj;
                    if (m == 0)
                    {
                        GridView1.Rows[i].Cells[2].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 1)
                    {
                        GridView1.Rows[i].Cells[3].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 2)
                    {
                        GridView1.Rows[i].Cells[4].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 3)
                    {
                        GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 4)
                    {
                        GridView1.Rows[i].Cells[6].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 5)
                    {
                        GridView1.Rows[i].Cells[7].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 6)
                    {
                        GridView1.Rows[i].Cells[8].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 7)
                    {
                        GridView1.Rows[i].Cells[9].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else if (m == 8)
                    {
                        GridView1.Rows[i].Cells[10].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                    else
                    {
                        GridView1.Rows[i].Cells[11].ForeColor = System.Drawing.Color.FromArgb(255, 000, 000);
                    }
                }
            }
            lbline.Text += "}</script>";
        }
    }
}